'use strict';

angular.module('xianwan')
	.factory('Notice', function ($rootScope, $window, IMConstant, Constant, Media, Message, LocalStorage, SettingConstant, AuthService, NW, SystemSetting, Xss) {

		let interceptMessageContent = function (message) {
			let content = Message.getSearchContent(message);
			if (message.body_type === IMConstant.BING_TEXT) {
				content = message.body.content;
			}
			if (message.body_type === IMConstant.BING_VOICE) {
				content = '[语音]';
			}
			if (!content) {
				return '未知类型';
			}
			let r = new RegExp('(<span){1}.*?(</span></span>){1}', 'g');
			content = content.replace(r, '[表情]');
			content = content.replace(/<br>/g, '');
			if (content.length > 20) {
				content = content.substring(0, 20) + '...';
			}
			return content;
		}

		let getAvatar = function (message) {
			let session = $rootScope.messages[message.identifier];
			if (message.body_type === IMConstant.BING_TEXT || message.body_type === IMConstant.BING_VOICE) {
				if (message.body.display_avatar) {
					return Media.download(message.body.display_avatar);
				}
				return Constant.APP_PUBLIC_URL + IMConstant.DEFAULT_PERSON_AVATAR;
			}
			if (session.avatar) {
				return Media.download(session.avatar);
			}
			if (session.type === IMConstant.USER) {
				return Constant.APP_PUBLIC_URL + IMConstant.DEFAULT_PERSON_AVATAR;
			}
			if (session.type === IMConstant.DISCUSSION) {
				return Constant.APP_PUBLIC_URL + IMConstant.DEFAULT_GROUP_AVATAR;
			}
			if (session.type === IMConstant.FRIENDSHIP_HELPER) {
				return Constant.APP_PUBLIC_URL + 'images/friend_new.jpg';
			}
			if (session.type === IMConstant.xianwan_SYSTEM_NOTICE) {
				return Constant.APP_PUBLIC_URL + 'images/icon_system_notice.png';
			}
			return Constant.APP_PUBLIC_URL + IMConstant.DEFAULT_PERSON_AVATAR;
		}

		let getName = function (message) {
			if (message.body_type === IMConstant.BING_TEXT || message.body_type === IMConstant.BING_VOICE) {
				return Xss.filter('来自' + message.body.my_name + '的必应消息')
			}
			if (!$rootScope.messages[message.identifier].name) {
				return '';
			}
			return Xss.filter($rootScope.messages[message.identifier].name.substring(0, 30) || '');
		}

		let macNotice = function (name, avatar, content, identifier, isBing) {
			let notice = window.showNotification(avatar, name, content, identifier, isBing);
			setTimeout(function () {
				notice.close();
			}, 5000);
		}

		let windowNotice = function (name, avatar, content, identifier, displayTime, message) {
			let notifier = null;
			let time = displayTime || 5000;

			try {
				notifier = require('nw-notify');
				notifier.setConfig({
					displayTime: time > 5000 ? 5000 : time,
					maxVisibleNotifications: 5
				});
			} catch (error) {
				console.error(error);
				if (error.message == "Cannot find module 'nw-notify'") {
					console.log("Can not load module 'nw-notify'.\nPlease run 'npm install'");
					return false;
				}
			}

			notifier.notify({
				title: name,
				text: content,
				image: avatar,
				tag: identifier,
				onClickFunc: function (event) {
					require('nw.gui').Window.get().show();

					if (message && (message.body_type === IMConstant.BING_TEXT || message.body_type === IMConstant.BING_VOICE)) {
						$rootScope.showBingDetail(this.tag);
						return;
					}

					$rootScope.openChatPanel(this.tag);

					var chatPane = angular.element("#chatMsgItemsPanes")[0];
					setTimeout(function () {
						chatPane.scrollTop = chatPane.scrollHeight;

						event.closeNotification();
					}, 150);
				},
				onShowFunc: function (event) {

				},
				onCloseFunc: function (event) {

				}
			});
		}

		let showNotice = function (message) {
			let content = interceptMessageContent(message);
			let avatar = getAvatar(message);
			let name = getName(message);

			if (!$rootScope.currentChat || $rootScope.currentChat.identifier !== message.identifier || !$window.showOrHide) {
				NW.setTrayIcon('true');
			}

			if (window.require) {
				window.messageIdentifier = message.identifier;
				if (process.platform == "darwin") {
					if (message.body_type === IMConstant.BING_TEXT || message.body_type === IMConstant.BING_VOICE) {
						message.isBing = true;
					}
					macNotice(name, avatar, content, message.identifier, message.isBing);
					return;
				}
				windowNotice(name, avatar, content, message.identifier, message.body.repeat_per_seconds * 1000, message);
			}
		}

		return {
			show: function (message) {
				if (message.body.emergency) {
					showNotice(message);
					return;
				}
				if (message.from === AuthService.currentUserId()) {
					return;
				}
				if ($rootScope.messages[message.identifier] && $rootScope.messages[message.identifier].remind == false) {
					return;
				}
				if (!$rootScope.currentChat || $rootScope.currentChat.identifier !== message.identifier || !$window.showOrHide) {
					NW.setTrayIcon('true');
				}
				if (SystemSetting.getSystemSettingByType(SettingConstant.NOTICE) === false) {
					return;
				}
				if ($window.showOrHide) {
					return;
				}
				let content = interceptMessageContent(message);
				let avatar = getAvatar(message);
				let name = getName(message);

				if (window.require) {
					window.messageIdentifier = message.identifier;
					if (process.platform == "darwin") {
						if (message.body_type === IMConstant.BING_TEXT || message.body_type === IMConstant.BING_VOICE) {
							message.isBing = true;
						}
						macNotice(name, avatar, content, message.identifier, message.isBing);
						return;
					}
					windowNotice(name, avatar, content, message.identifier, null, message);
				}
			}
		}
	});